//convert.cpp
#include "convert.h"

#ifdef WINDOWS

int StringConvert::Construct()
{
	return 0;
}
int StringConvert::Destruct()
{
	m_objWstrBuf.reserve(0);
	return 0;
}

int StringConvert::Init(int iWstrBufLen)
{
	int iRet=-1,i;
	int iResult,iTemp;

	if(iWstrBufLen<=0) SET_RET_AND_GOTO_EXIT(COMMON_ERR_INVALIDARGS);
	m_objWstrBuf.reserve(iWstrBufLen);

	iRet=0;
_Exit:
	return iRet;
}

int StringConvert::GbkToUtf8(
		IN const char *pcGbk,
		IN int iGbkLen,
		OUT char *pcUtf8,
		IN OUT int &iUtf8Len)
{
	int iRet=-1,i;
	int iResult,iTemp;
	int iWcharLen;

	if(m_objWstrBuf.capacity()<=0) SET_RET_AND_GOTO_EXIT(COMMON_ERR_UNINITED);
	if(pcGbk==NULL||pcUtf8==NULL||iUtf8Len<=0) SET_RET_AND_GOTO_EXIT(COMMON_ERR_INVALIDARGS);

	iResult = MultiByteToWideChar(CP_ACP, 0, pcGbk, iGbkLen, NULL, 0);
	if(iResult>m_objWstrBuf.capacity())
	{
		iUtf8Len=iResult;
		SET_RET_AND_GOTO_EXIT(STRCVT_ERR_NOMEM);
	}
	else if(iResult<=0) goto _Exit;

	m_objWstrBuf.resize(iResult);
	iResult = MultiByteToWideChar(CP_ACP, 0, pcGbk, iGbkLen, &m_objWstrBuf[0], iResult);
	if(iResult<=0) goto _Exit;
	iWcharLen=iResult;

	iResult = WideCharToMultiByte(CP_UTF8, 0, &m_objWstrBuf[0], iWcharLen, NULL, 0, NULL, NULL);
	if(iResult>iUtf8Len)
	{
		iUtf8Len=iResult;
		SET_RET_AND_GOTO_EXIT(COMMON_ERR_NOMEM);
	}
	else if(iResult<=0) goto _Exit;

	iResult = WideCharToMultiByte(CP_UTF8, 0, &m_objWstrBuf[0], iWcharLen, pcUtf8, iUtf8Len, NULL, NULL);
	if(iResult<=0) goto _Exit;
	iUtf8Len=iResult;

	iRet=0;
_Exit:
	return iRet;
}

#endif